1547C - Pair Programming - CodeForces Solution


greedy two pointers *1100

Please click on ads to support us..

Python Code:

def solve(s, a, b):
    k = s[0]
    indexA = 0
    indexB = 0
    l = []
    for i in range(s[1] + s[2]):
        if indexA == s[1] and indexB < s[2]:
            while indexB < s[2]:
                if b[indexB] == 0:
                    k += 1
                    l.append(0)
                    indexB += 1
                elif b[indexB] <= k:
                    l.append(b[indexB])
                    indexB += 1
                else:
                    return -1

        elif indexB == s[2] and indexA < s[1]:
            while indexA < s[1]:
                if a[indexA] == 0:
                    k += 1
                    l.append(0)
                    indexA += 1
                elif a[indexA] <= k:
                    l.append(a[indexA])
                    indexA += 1
                else:
                    return -1

        if indexA != s[1] and indexB != s[2]:
            if a[indexA] < b[indexB]:
                if a[indexA] == 0:
                    k += 1
                    l.append(0)
                    indexA += 1
                elif a[indexA] <= k:
                    l.append(a[indexA])
                    indexA += 1
                else:
                    return -1

            elif a[indexA] >= b[indexB]:
                if b[indexB] == 0:
                    k += 1
                    l.append(0)
                    indexB += 1
                elif b[indexB] <= k:
                    l.append(b[indexB])
                    indexB += 1
                else:
                    return -1

    return l


t = int(input())

for i in range(t):
    input()
    s = [int(j) for j in input().split()]
    a = [int(j) for j in input().split()]
    b = [int(j) for j in input().split()]
    l = solve(s, a, b)
    if l != -1:
        for i in l:
            print(i, end=" ")
        print()
    else:
        print(-1)

C++ Code:

#include<bits/stdc++.h>

using namespace std;

typedef pair<int,int>PII;

const int N=200010;

int a[N],b[N];
int H,d,n,k,w,m;

signed main()
{
    int T;
    cin>>T;
    while(T--)
    {
        bool f=1;
        cin>>k>>n>>m;
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        for(int i=1;i<=m;i++)scanf("%d",&b[i]);
        vector<int>v;
        int i=0,j=0;
        while(i<=n||j<=m)
        {
            i++,j++;
            
            while(a[i]==0&&i<=n)i++,k++,v.push_back(0);
            while(b[j]==0&&j<=m)j++,k++,v.push_back(0);
            
            if(i<=n&&j<=m)
            {
                if(a[i]<=b[j])
                {
                    if(a[i]>k)f=0;
                    v.push_back(a[i]);
                    j--;
                }
                else
                {
                    if(b[j]>k)f=0;
                    v.push_back(b[j]);
                    i--;
                }
            }
            else if(i<=n&&j>m)
            {
                if(a[i]>k)f=0;
                v.push_back(a[i]);
            }
            else if(j<=m&&i>n)
            {
                if(b[j]>k)f=0;
                v.push_back(b[j]);
            }
        }
        
        if(f)
        {
            for(auto it:v)cout<<it<<' ';
            cout<<endl;
        }
        else puts("-1");
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST